package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Defaultaccess;

@Repository(value = "defaultAccessDao")
@Transactional(readOnly = false)
public class DefaultAccessDaoImpl implements DefaultAccessDao {
	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	} 
	
	@SuppressWarnings("unchecked")
	public List<Defaultaccess> getDefaultAccessList() {
		return em.createNamedQuery("Defaultaccess.findAll").getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Defaultaccess> getDefaultAccessList(Integer rolId) {
		Query  q =em.createNamedQuery("Defaultaccess.findByRolId");
		q.setParameter("rolId",rolId);		
    	return q.getResultList();
	}
	@SuppressWarnings("unchecked")
	public List<Defaultaccess> getDefaultAccessList(Integer rolId,short menuId, short submenuId) {
		Query  q =em.createNamedQuery("Defaultaccess.findByRolIdByMenuIdBySubmenuId");
		q.setParameter("rolId",rolId);		
		q.setParameter("menuId",menuId);		
		q.setParameter("submenuId",submenuId);		
    	return q.getResultList();
	}

	public Defaultaccess findByRolIdByMenuIdBySubMenuIdByOptionId(Integer rolId, short menuId, short submenuId, short optionId) {
		Defaultaccess res=null;
    	Query  q = em.createNamedQuery("Defaultaccess.findByRolIdByMenuIdBySubmenuIdByOptionId");
    	q.setParameter("rolId",rolId);		
		q.setParameter("menuId",menuId);		
		q.setParameter("submenuId",submenuId);
		q.setParameter("optionId",optionId);
    	if(q.getResultList().size()==1)
    		res=(Defaultaccess)q.getSingleResult();
    	return res;
	}
}
